20180421 終於開始學習如何下載拉
最單純的request.get的下載方式
import requests #導入模組
url="http://photo.beautyleg.com/album/96-tAJhE5/0000.jpg" #測試連結
file_path="Downloads\img1.jpg" #檔案儲存位置 (以python位置為初始目錄)
r = requests.get(url)
with open(file_path, "wb") as code:
code.write(r.content)
當下載檔案過大時會遇到內存不足,則要使用stream的方式,如下:
#將檔案分以32個位元(?)為一組連續下載
r = requests.get(url, stream=True) #stream_loading
with open(file_path, "wb") as code:
for chunk in r.iter_content(chunk_size=32): #使用iter_content open (split 32 bytes)
code.write(chunk)